package cn.tedu.baking.service.impl;

import cn.tedu.baking.mapper.ContentMapper;
import cn.tedu.baking.pojo.dto.ContentDTO;
import cn.tedu.baking.pojo.entity.Content;
import cn.tedu.baking.pojo.vo.*;
import cn.tedu.baking.service.IContentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.List;

@Service
public class ContentService implements IContentService {
    @Autowired
    ContentMapper mapper;
    @Override
    public void addNew(ContentDTO contentDTO) {
        Content content = new Content();
        BeanUtils.copyProperties(contentDTO,content);

        if(contentDTO.getId()==null){//添加
            content.setCreateTime(new Date());
            System.out.println(content);
            mapper.insert(content);
        }else{//修改
            content.setUpdateTime(new Date());
            mapper.update(content);
        }





    }

    @Override
    public List<ContentManagementVO> listForManagementByType(Long type,Long id) {
        System.out.println("type = " + type + ", id = " + id);

        return mapper.selectForManagementByType(type,id);
    }
    @Value("${filePath}")
    private String dirPath;

    @Override
    public void deleteById(Long id) {
        //得到封面的路径 然后删除
        String imgUrl = mapper.selectUrlById(id);
        new File(dirPath+imgUrl).delete();
        mapper.deleteById(id);
    }

    @Override
    public ContentUpdateVO selectForUpdateById(Long id) {
        return mapper.selectForUpdateById(id);
    }

    @Override
    public List<ContentIndexVO> listForIndex(Long type, Long categoryId) {
        return mapper.selectForIndexByTypeAndCategoryId(type,categoryId);
    }

    @Override
    public ContentDetailVO getDetailById(Long id) {
        //让浏览量+1
        mapper.updateViewCountById(id);

        return mapper.selectForDetailById(id);
    }

    @Override
    public List<ContentSimpleVO> listForOtherByUserId(Long id) {
        return mapper.selectForOtherByUserId(id);
    }

    @Override
    public List<ContentSimpleVO> listForHot() {
        return mapper.selectForHot();
    }

    @Override
    public List<ContentIndexVO> listByType(Long type) {
        return mapper.selectListByType(type);
    }

    @Override
    public List<ContentIndexVO> listByWd(String wd) {
        return mapper.selectListByWd(wd);
    }
    @Override
    public List<ContentAdminVO> listForAdminByType(Long type) {
        return mapper.selectListForAdminByType(type);
    }
}
